﻿Imports DevExpress
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Public Class UcnCombobox
    'represents combobox implemented with treelist

#Region "Attributes"
#Region "Private attributes"
    Private MbReadyForUse As Boolean = False 'indicates proper creation or first initialization of usercontrol
    Private MoSelectedRow As TreeListNode = Nothing 'actually selected row
#End Region
#End Region
#Region "Constructors & Init"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        '**********************************************************************
        InitControl()
        '**********************************************************************
    End Sub
    'setting usercontrol settings before its using
    Private Function InitControl() As GeActionResult
        SetReadyForUse(False) 'disabling usercontrol
        Try
            Me.tlTreeList.Nodes.Clear()
            MoSelectedRow = Me.tlTreeList.FocusedNode

            SetReadyForUse(True) 'enabling usercontrol
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'initialization of controls
    Public Function Init(ByVal IsCaptionText As String, ByVal IoTextCollection As Collection, Optional ByVal IiSelectedRow As Integer = 0, Optional ByVal IoIdCollection As Collection = Nothing) As GeActionResult
        Try
            If MbReadyForUse Then
                Dim LbIsIdList As Boolean = False
                Dim LoIdCollection As Collection = New Collection

                Me.tlTreeList.ClearSorting()
                Me.tlTreeList.Columns("PropertyValue").Caption = IsCaptionText

                If IoIdCollection IsNot Nothing Then
                    If IoTextCollection.Count <> IoIdCollection.Count Then
                        LogEvent("Nespravny pocet prvkov kolekcii - UcnCombobox!", , )
                        LbIsIdList = False
                    Else
                        LbIsIdList = True
                    End If
                Else
                    LbIsIdList = False
                End If

                'creation of new list with ID for values
                If Not LbIsIdList Then
                    LoIdCollection.Clear()
                    For LiI As Integer = 0 To IoTextCollection.Count - 1
                        LoIdCollection.Add(LiI)
                    Next
                Else
                    LoIdCollection = IoIdCollection
                End If

                Dim LoNode As TreeListNode = Nothing
                Dim LoSelectedNode As TreeListNode = Nothing
                Me.tlTreeList.Nodes.Clear()
                For LiI As Integer = 0 To IoTextCollection.Count - 1
                    LoNode = Me.tlTreeList.AppendNode(New Object() {IoTextCollection(LiI + 1).ToString, LoIdCollection(LiI + 1), LiI}, -1)
                    If LiI = IiSelectedRow Then
                        LoSelectedNode = LoNode
                    End If
                Next

                'selecting row
                Me.tlTreeList.FocusedNode = LoSelectedNode

                Return GeActionResult.kkOk
            Else
                LogEvent("Neinicializovany UcnCombobox!", , )
                Return GeActionResult.kkError
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Properties"
#Region "Public properties"
    Public ReadOnly Property ReadyForUse() As Boolean
        Get
            Return MbReadyForUse
        End Get
    End Property
    'result of this "control"
    Public ReadOnly Property Result() As Object
        Get
            If MoSelectedRow IsNot Nothing Then
                Return MoSelectedRow.GetValue("PropertyID")
            Else
                Return Nothing
            End If
        End Get
    End Property
#End Region
#End Region
#Region "Events handler"
    'drawing a cell
    Private Sub tlTreeList_CustomDrawNodeCell(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles tlTreeList.CustomDrawNodeCell
        If e.Node.Selected Then
            e.Appearance.BackColor = Me.tlTreeList.Appearance.FocusedCell.BackColor
            ''DOCASNE
            e.Appearance.ForeColor = Color.White
        End If
    End Sub
    'keypressed - confirmation of result
    Private Sub tlTreeList_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tlTreeList.KeyPress
        If Asc(e.KeyChar) <> Keys.Escape Then 'ESC
            MoSelectedRow = Me.tlTreeList.FocusedNode
            PopupContainerControl.OwnerEdit.ClosePopup()
        End If
    End Sub
    'mouseclick - confirmation of result
    Private Sub tlTreeList_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tlTreeList.MouseClick
        Dim LoNode As TreeListNode = Me.tlTreeList.CalcHitInfo(New Point(e.X, e.Y)).Node
        If (e.Button = Windows.Forms.MouseButtons.Left) And (LoNode IsNot Nothing) Then
            MoSelectedRow = LoNode
            PopupContainerControl.OwnerEdit.ClosePopup()
        End If
    End Sub
    'mouse move - focusing node under mouse pointer
    Private Sub tlTreeList_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tlTreeList.MouseMove
        Dim LoHitInfo As TreeListHitInfo = Me.tlTreeList.CalcHitInfo(New Point(e.X, e.Y))
        If LoHitInfo.Node IsNot Nothing Then
            Me.tlTreeList.FocusedNode = LoHitInfo.Node
        End If
    End Sub
#End Region
#Region "Functions"
    'gets PropertyID by PropertyValue
    Public Function GetPropertyID(ByVal IoPropertyValue As Object) As Object
        Try
            For Each LoNode As TreeListNode In Me.tlTreeList.Nodes
                If LoNode("PropertyValue") = IoPropertyValue Then Return LoNode.GetValue("PropertyID")
            Next

            Return Nothing
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets PropertyValue by PropertyID
    Public Function GetPropertyValue(ByVal IoPropertyID As Object) As Object
        Try
            If IoPropertyID IsNot Nothing And Not IsDBNull(IoPropertyID) Then
                For Each LoNode As TreeListNode In Me.tlTreeList.Nodes
                    If LoNode("PropertyID") = IoPropertyID Then Return LoNode.GetValue("PropertyValue")
                Next
            End If

            Return "???"
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return "???"
        End Try
    End Function
    'select node
    Public Function SelectRowByPropertyID(ByVal IoPropertyID As Object) As GeActionResult
        Try
            For Each LoNode As TreeListNode In Me.tlTreeList.Nodes
                If LoNode("PropertyID") = IoPropertyID Then
                    tlTreeList.FocusedNode = LoNode
                    MoSelectedRow = tlTreeList.FocusedNode
                    Return GeActionResult.kkOk
                End If
            Next

            tlTreeList.FocusedNode = Nothing
            MoSelectedRow = tlTreeList.FocusedNode
            Return GeActionResult.kkError
        Catch ex As Exception
            LogEventFromException(ex, , , )
            tlTreeList.FocusedNode = Nothing
            MoSelectedRow = tlTreeList.FocusedNode
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Subs"
    'sets MbReadyForUse attribute and sets Enable property of usercontrol
    Private Sub SetReadyForUse(ByVal IbValue As Boolean)
        MbReadyForUse = IbValue
        Me.Enabled = IbValue 'enabling usercontrol
    End Sub
#End Region

End Class